package com.css.fxfzypg.manage.lethalitydifference.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.css.fxfzypg.base.attachment.entity.AttachmentInfoEntity;
import com.css.fxfzypg.base.attachment.service.AttachmentInfoService;
import com.css.fxfzypg.base.exception.ServiceException;
import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.logInfo.SysLog;
import com.css.fxfzypg.manage.lethalitydifference.repository.entity.LethalityDifferenceEntity;
import com.css.fxfzypg.manage.lethalitydifference.service.LethalityDifferenceService;
import com.css.fxfzypg.manage.lethalitydifference.vo.LethalityDifferenceParams;
import com.css.fxfzypg.util.PlatformObjectUtils;
import com.css.fxfzypg.util.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/lethalityDifference")
@Slf4j
public class LethalityDifferenceController {

    @Autowired
    LethalityDifferenceService lethalityDifferenceService;

    /**
     * 新增数据   测试用
     *
     * @param
     * @return
     */
    @GetMapping("/saveDataByTask")
    public RestResponse saveDataByTask(String taskCode, String taskName, String taskDistrict) {
        RestResponse response = null;
        try {
            boolean res = lethalityDifferenceService.saveDataByTask(taskCode, taskName, taskDistrict);
            if (res) {
                response = RestResponse.succeed("保存成功!");
                response.setMessage("保存成功!");
            } else {
                response = RestResponse.fail("保存失败!");
            }
        } catch (Exception e) {
            String errormessage = "保存失败!";
            log.error(errormessage, e);
            response = RestResponse.fail(errormessage);
        }
        return response;
    }


    /**
     * 新增数据
     *
     * @param lethalityDifferenceEntity
     * @return
     */
    @PostMapping("/save")
    public RestResponse save(@RequestBody LethalityDifferenceEntity lethalityDifferenceEntity) {
        RestResponse response = null;
        try {
            boolean res = lethalityDifferenceService.save(lethalityDifferenceEntity);
            if (res) {
                response = RestResponse.succeed("保存成功!");
                response.setMessage("保存成功!");
            } else {
                response = RestResponse.fail("保存失败!");
            }
        } catch (Exception e) {
            String errormessage = "保存失败!";
            log.error(errormessage, e);
            response = RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     * app端保存并提交
     *
     * @param lethalityDifferenceEntity
     * @return
     */
    @PostMapping("/saveAndSubmit")
    public RestResponse saveAndSubmit(@RequestBody LethalityDifferenceEntity lethalityDifferenceEntity) {
        RestResponse response = null;
        try {
            boolean res = lethalityDifferenceService.saveAndSubmit(lethalityDifferenceEntity);
            if (res) {
                response = RestResponse.succeed("保存并提交成功!");
                response.setMessage("保存并提交成功!");
            } else {
                response = RestResponse.fail("保存并提交失败!");
            }
        } catch (Exception e) {
            String errormessage = "保存并提交失败!";
            log.error(errormessage, e);
            response = RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     * PC端保存并提交(和app保存区别为:app传省市县的名字,pc传省市县的id)
     *
     * @param lethalityDifferenceEntity
     * @return
     */
    @PostMapping("/saveAndSubmitPC")
    public RestResponse saveAndSubmitPC(@RequestBody LethalityDifferenceEntity lethalityDifferenceEntity) {
        RestResponse response = null;
        try {
            boolean res = lethalityDifferenceService.saveAndSubmitPC(lethalityDifferenceEntity);
            if (res) {
                response = RestResponse.succeed("保存并提交成功!");
                response.setMessage("保存并提交成功!");
            } else {
                response = RestResponse.fail("保存并提交失败!");
            }
        } catch (Exception e) {
            String errormessage = "保存并提交失败!";
            log.error(errormessage, e);
            response = RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     * 修改
     *
     * @param lethalityDifferenceEntity
     * @return
     */
    @PostMapping("/modifyLethalityDifference")
    public RestResponse modifyLethalityDifference(@RequestBody LethalityDifferenceEntity lethalityDifferenceEntity) {
        RestResponse response = null;
        try {
            boolean res = lethalityDifferenceService.modifyLethalityDifference(lethalityDifferenceEntity);
            if (res) {
                response = RestResponse.succeed("修改成功!");
                response.setMessage("修改成功!");
            } else {
                response = RestResponse.fail("修改失败!");
            }
        } catch (Exception e) {
            String errormessage = "修改失败!";
            log.error(errormessage, e);
            response = RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     * 修改并提交
     *
     * @param lethalityDifferenceEntity
     * @return
     */
    @PostMapping("/modifyAndSubmit")
    public RestResponse modifyAndSubmit(@RequestBody LethalityDifferenceEntity lethalityDifferenceEntity) {
        RestResponse response = null;
        try {
            boolean res = lethalityDifferenceService.modifyAndSubmit(lethalityDifferenceEntity);
            if (res) {
                response = RestResponse.succeed("修改并提交成功!");
                response.setMessage("修改并提交成功!");
            } else {
                response = RestResponse.fail("修改并提交失败!");
            }
        } catch (Exception e) {
            String errormessage = "修改并提交失败!";
            log.error(errormessage, e);
            response = RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     * 查询已提交和未提交列表
     *
     * @param request
     * @param submitFlag
     * @param taskCode
     * @return
     */
    @GetMapping("/querySubmitOrNot")
    public RestResponse querySubmitOrNot(HttpServletRequest request, String submitFlag, String taskCode) {
        RestResponse response = null;
        try {
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String, Object> map = lethalityDifferenceService.querySubmitOrNot(submitFlag, taskCode, curPage, pageSize);
            response = RestResponse.succeed(map);
            response.setMessage("获取列表信息成功!");
        } catch (Exception e) {
            log.error("获取列表信息失败!", e);
            response = RestResponse.fail("获取列表信息失败!");
        }
        return response;
    }

    /**
     * 查询app端已提交和未提交列表
     *
     * @param request
     * @param submitFlag
     * @param taskCode
     * @return
     */
    @GetMapping("/appQuerySubmitOrNot")
    public RestResponse appQuerySubmitOrNot(HttpServletRequest request, String submitFlag, String taskCode,String userId) {
        RestResponse response = null;
        try {
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String, Object> map = lethalityDifferenceService.appQuerySubmitOrNot(submitFlag, taskCode, curPage, pageSize,userId);
            response = RestResponse.succeed(map);
            response.setMessage("获取列表信息成功!");
        } catch (Exception e) {
            log.error("获取列表信息失败!", e);
            response = RestResponse.fail("获取列表信息失败!");
        }
        return response;
    }



    /**
     * 根据id更新提交状态
     *
     * @param
     * @return
     */
    @PostMapping("/updateIsSubmit")
    public RestResponse updateIsSubmit(@RequestBody Map<String, String> map) {
        RestResponse response = null;
        try {
            boolean res = lethalityDifferenceService.updateIsSubmit(map);
            if (res) {
                response = RestResponse.succeed("提交成功!");
                response.setMessage("提交成功!");
            } else {
                response = RestResponse.fail("提交失败!");
            }
        } catch (Exception e) {
            String errormessage = "提交失败!";
            log.error(errormessage, e);
            response = RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     * 根据id查询详情页数据
     *
     * @param id
     * @return
     */
    @GetMapping("/queryData")
    public RestResponse queryData(String id) {
        RestResponse response = null;
        try {
            JSONObject json = lethalityDifferenceService.queryData(id);
            response = RestResponse.succeed(json);
            response.setMessage("查询详情页成功!");
        } catch (Exception e) {
            String errorMessage = "查询详情页失败";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 数据列表查询
     *
     * @param request
     * @param lethalityDifferenceParams
     * @return
     */
    @GetMapping("/queryDataList")
    public RestResponse queryDataList(HttpServletRequest request, LethalityDifferenceParams lethalityDifferenceParams) {
        RestResponse response = null;
        try {
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String, Object> map = lethalityDifferenceService.queryDataList(request, lethalityDifferenceParams, curPage, pageSize);
            response = RestResponse.succeed(map);
            response.setMessage("查询区县致死性差异性指标列表数据成功!");
        } catch (Exception e) {
            log.error("查询区县致死性差异性指标列表数据失败!", e);
            response = RestResponse.fail("查询区县致死性差异性指标列表数据失败!");
        }
        return response;
    }

    /**
     * 更改采纳状态 0:未采纳,1:已采纳,2:已入库
     *
     * @param map
     * @return
     */
    @PostMapping("/updateIsAdopt")
    public RestResponse updateIsAdopt(@RequestBody Map<String, String> map) {
        RestResponse response = null;
        String isAdopt = map.get("isAdopt");
        try {
            Map<String, Object> resMap = lethalityDifferenceService.updateIsAdopt(map);
            if (resMap != null && resMap.size() > 0) {
                boolean res = ((Boolean) resMap.get("res")).booleanValue();
                if (isAdopt.equals("0")) {
                    if (resMap.get("res") != null) {
                        if (res) {
                            response = RestResponse.succeed("取消采纳成功!");
                            response.setMessage("取消采纳成功!");
                        } else {
                            response = RestResponse.fail("取消采纳失败!");
                        }
                    }
                }
                if (isAdopt.equals("1")) {
                    if (resMap.get("message") != null) {
                        response = RestResponse.succeed("采纳失败!");
                        response.setMessage(resMap.get("message").toString());
                    } else {
                        if (resMap.get("res") != null) {
                            if (res) {
                                response = RestResponse.succeed("采纳成功!");
                                response.setMessage("采纳成功!");
                            } else {
                                response = RestResponse.fail("采纳失败!");
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            if (isAdopt.equals("0")) {
                log.error("取消采纳失败!", e);
                response = RestResponse.fail("取消采纳失败!");
            }
            if (isAdopt.equals("1")) {
                log.error("采纳失败!", e);
                response = RestResponse.fail("采纳失败!");
            }
        }
        return response;
    }

    /**
     * 根据id删除
     *
     * @param map
     * @return
     */
    @PostMapping("/deleteById")
    public RestResponse deleteById(@RequestBody Map<String, String> map) {
        RestResponse response = null;
        try {
            boolean res = lethalityDifferenceService.deleteById(map.get("id"));
            if (res) {
                response = RestResponse.succeed("删除成功!");
                response.setMessage("删除成功!");
            } else {
                response = RestResponse.fail("删除失败!");
            }
        } catch (Exception e) {
            log.error("删除失败!", e);
            response = RestResponse.fail("删除失败!");
        }
        return response;
    }

    /**
     * 已入库数据列表查询(模型管理菜单下)
     *
     * @param request
     * @param lethalityDifferenceParams
     * @return
     */
    @GetMapping("/queryPutDataList")
    public RestResponse queryPutDataList(HttpServletRequest request, LethalityDifferenceParams lethalityDifferenceParams) {
        RestResponse response = null;
        try {
            int curPage = Integer.parseInt(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.parseInt(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String, Object> map = lethalityDifferenceService.queryPutDataList(request, lethalityDifferenceParams, curPage, pageSize);
            response = RestResponse.succeed(map);
            response.setMessage("查询区县致死性差异性指标已入库列表数据成功!");
        } catch (Exception e) {
            log.error("查询区县致死性差异性指标已入库列表数据失败!", e);
            response = RestResponse.fail("查询区县致死性差异性指标已入库列表数据失败!");
        }
        return response;
    }

    /**
     * 根据id查询详情页数据(模型管理 维护权重值的详情页)
     *
     * @param id
     * @return
     */
    @GetMapping("/getDataById")
    public RestResponse getDataById(String id) {
        RestResponse response = null;
        try {
            JSONObject json = lethalityDifferenceService.getDataById(id);
            response = RestResponse.succeed(json);
            response.setMessage("查询详情页成功!");
        } catch (Exception e) {
            String errorMessage = "查询详情页失败";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 维护权重值
     *
     * @param map
     * @return
     */
    @PostMapping("/addWeightValue")
    public RestResponse addWeightValue(@RequestBody Map<String, Object> map) {
        RestResponse response = null;
        try {
            boolean res = lethalityDifferenceService.addWeightValue(map);
            if (res) {
                response = RestResponse.succeed("添加成功!");
                response.setMessage("添加成功!");
            } else {
                response = RestResponse.fail("添加失败!");
            }
        } catch (Exception e) {
            String errormessage = "添加失败!";
            log.error(errormessage, e);
            response = RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     * 生成区县致死性差异性分布
     *
     * @return
     */
    @SysLog("生成区县致死性差异性分布")
    @PostMapping("/createFinalValue")
    public RestResponse createFinalValue(String taskDistrict, String taskNum) {
        RestResponse response = null;
        try {
            Map<Object, Object> map = lethalityDifferenceService.createFinalValue(taskDistrict, taskNum);
            boolean res = ((Boolean) map.get("res")).booleanValue();
            if (res) {
                response = RestResponse.succeed("生成区县致死性差异性分布成功!");
                response.setMessage("生成区县致死性差异性分布成功!");
            } else {
                String mess = (String) map.get("mess");
                response = RestResponse.fail(503, "生成区县致死性差异性分布失败!");
                response.setMessage(mess);
            }
        } catch (Exception e) {
            String errorMessage = "生成区县致死性差异性分布失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 获取区县致死性差异性分布数据
     *
     * @return
     */
    @GetMapping("/getFinalValue")
    public RestResponse getFinalValue(String taskId) {
        RestResponse response = null;
        try {
            List<LethalityDifferenceEntity> list = lethalityDifferenceService.getFinalValue(taskId);
            response = RestResponse.succeed(list);
            response.setMessage("获取区县致死性差异性分布数据成功!");
        } catch (Exception e) {
            log.error("获取区县致死性差异性分布数据失败!", e);
            response = RestResponse.fail("获取区县致死性差异性分布数据失败!");
        }
        return response;
    }


    /**
     * 根据id删除自定义数据表
     *
     * @param map
     * @return
     */
    @PostMapping("/deleteCustomIndicatorById")
    public RestResponse deleteCustomIndicatorById(@RequestBody Map<String, String> map) {
        RestResponse response = null;
        try {
            boolean res = lethalityDifferenceService.deleteCustomIndicatorById(map.get("id"));
            if (res) {
                response = RestResponse.succeed("删除自定义成功!");
                response.setMessage("删除自定义成功!");
            } else {
                response = RestResponse.fail("删除自定义失败!");
            }
        } catch (Exception e) {
            log.error("删除自定义失败!", e);
            response = RestResponse.fail("删除自定义失败!");
        }
        return response;
    }

    /**
     * 导入excel
     *
     * @param file
     * @return
     */
    @SysLog("上传区县致死性差异性")
    @PostMapping("/uploadLethalityDifference")
    public RestResponse uploadLethalityDifference(@RequestParam("file") MultipartFile file) {
        RestResponse response = null;
        try {
            Map<String, Object> map = lethalityDifferenceService.uploadLethalityDifference(file);
            boolean res = ((Boolean) map.get("res")).booleanValue();
            ;
            if (res) {
                response = RestResponse.succeed("上传成功!");
                response.setMessage("上传成功!");
            } else {
                response = RestResponse.fail("上传失败!");
                response.setMessage(map.get("mess").toString());
            }
        } catch (ServiceException se) {
            log.error(se.getMessage(), se);
            return RestResponse.fail(se.getMessage());
        } catch (Exception e) {
            String errormessage = "上传失败!";
            log.error(errormessage, e);
            response = RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     * excel导出
     *
     * @param lethalityDifferenceParams
     * @throws Exception
     */
    @SysLog("导出区县致死性差异性")
    @PostMapping("/exportLethalityDifference")
    public RestResponse exportLethalityDifference(HttpServletResponse response, LethalityDifferenceParams lethalityDifferenceParams) {
        RestResponse restResponse = null;
        try {
            lethalityDifferenceService.exportLethalityDifference(response, lethalityDifferenceParams);
            RestResponse.succeed("导成出功");
        } catch (Exception e) {
            String errormessage = "导出失败!";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }


    /**
     * 任务结束前调用  校验是否采纳
     *
     * @param taskId
     * @return
     */
    @GetMapping("/checkIsAdopted")
    public RestResponse checkIsAdopted(String taskId) {
        RestResponse restResponse = null;
        try {
            Map<String, Object> map = lethalityDifferenceService.checkIsAdopted(taskId);
            boolean res = ((Boolean) map.get("res")).booleanValue();
            if (res) {
                restResponse = RestResponse.succeed(res);
                restResponse.setMessage("校验成功!");
            } else {
                restResponse = RestResponse.succeed(res);
                restResponse.setMessage(map.get("mess").toString());
            }
        } catch (Exception e) {
            String errorMessage = "校验失败!";
            log.error(errorMessage, e);
            restResponse = RestResponse.fail(errorMessage);
        }
        return restResponse;
    }

    /**
     * 任务结束前调用  进行入库校验
     *
     * @param taskNum
     * @return
     */
    @GetMapping("/checkPutLethalityDifference")
    public RestResponse checkPutLethalityDifference(String taskNum) {
        RestResponse restResponse = null;
        try {
            JSONArray jsonArray = lethalityDifferenceService.checkPutLethalityDifference(taskNum);
            restResponse = RestResponse.succeed(jsonArray);
            restResponse.setMessage("校验成功!");
        } catch (Exception e) {
            String errorMessage = "校验失败!";
            log.error(errorMessage, e);
            restResponse = RestResponse.fail(errorMessage);
        }
        return restResponse;
    }

    /**
     * 任务结束前调用  入库校验  无返回值
     * 更新状态(已采纳改为已入库)
     *
     * @param map
     * @return
     */
    @PostMapping("/updateIsAdoptByTaskNum")
    public RestResponse updateIsAdoptByTaskNum(@RequestBody Map<String, String> map) {
        RestResponse restResponse = null;
        try {
            boolean res = lethalityDifferenceService.updateIsAdoptByTaskNum(map.get("taskNum"));
            if (res) {
                restResponse = RestResponse.succeed(res);
                restResponse.setMessage("更新成功!");
            } else {
                restResponse = RestResponse.succeed("任务下未存在已采纳数据,更新失败!");
            }
        } catch (Exception e) {
            String errorMessage = "更新失败!";
            log.error(errorMessage, e);
            restResponse = RestResponse.fail(errorMessage);
        }
        return restResponse;
    }

    /**
     * 任务结束前调用  入库校验 有返回值
     * 更新状态(将已入库改为已采纳,已采纳改为已入库)
     *
     * @param map
     * @return
     */
    @PostMapping("/updateIsAdoptById")
    public RestResponse updateIsAdoptById(@RequestBody Map<String, String> map) {
        RestResponse restResponse = null;
        try {
            boolean res = lethalityDifferenceService.updateIsAdoptById(map.get("rkId"), map.get("cnId"));
            if (res) {
                restResponse = RestResponse.succeed(res);
                restResponse.setMessage("更新成功!");
            } else {
                restResponse = RestResponse.fail("更新失败!");
            }
        } catch (Exception e) {
            String errorMessage = "更新失败!";
            log.error(errorMessage, e);
            restResponse = RestResponse.fail(errorMessage);
        }
        return restResponse;
    }

}
